﻿using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
using System;

namespace ugui.demo1
{
    //ICanvasRaycastFilter可以控制射线是否穿透物品，也可以使用CanvasGroup的blocksRaycasts 
    public class pointerCurrentRaycast : MonoBehaviour,
        IPointerUpHandler,
        IBeginDragHandler,
        IDragHandler,
        IEndDragHandler,
        ICanvasRaycastFilter
    {
        private bool isRaycastLocationValid = true;//默认射线不能穿透物品 
        
        //鼠标指针从该Widget刚进入触发
        public void OnPointerUp(PointerEventData eventData)
        {



            GameObject go = eventData.pointerCurrentRaycast.gameObject;

            if (go != null)
            {
                Debug.LogError(gameObject.name + " OnPointerUp eventData.pointerCurrentRaycast:" + go.name);
            }
        }

        public void OnBeginDrag(PointerEventData eventData)
        {

            isRaycastLocationValid = false;
        }

        public void OnDrag(PointerEventData eventData)
        {
            //transform.position = Input.mousePosition;
            GameObject go = eventData.pointerCurrentRaycast.gameObject;

            if (go != null)
            {
                Debug.LogError(gameObject.name + " OnDrag eventData.pointerCurrentRaycast:" + go.name);
            }
        }

        public void OnEndDrag(PointerEventData eventData)
        {
            GameObject go = eventData.pointerCurrentRaycast.gameObject;

            if (go != null)
            {
                Debug.LogError(gameObject.name + " OnEndDrag eventData.pointerCurrentRaycast:" + go.name);
            }
            else
            {
                
            }
            isRaycastLocationValid = true;
        }

        public bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera)
        {
            return isRaycastLocationValid;
        }
    }
}
